package alpaca.database;

import java.sql.SQLException;
import java.util.Date;

import alpaca.core.Contact;
import alpaca.core.ContactList;
import alpaca.core.IUser;
import alpaca.core.Message;
import alpaca.core.MessageList;
import alpaca.core.UserList;

/**
 * Interface for a database implementation to implement.
 * These are all methods to offer the functionality the
 * IServerModel promises.
 */

public interface IDatabaseSystem {

	void createConnection(String db, String user, String pass) throws SQLException;
	/**
	 * If a table doesn't exist, create it.
	 */
	void createTables() throws SQLException;
	/**
	 * Closes the connection to the database.
	 * @throws SQLException
	 */
	void closeConnection() throws SQLException;
	
	/***** Search methods ******/
	
	/**
	 * Returns a user object by email.
	 * Doesn't include the password.
	 * The user must be active (not removed).
	 */
	IUser getUserByEmail(String email) throws SQLException;
	/**
	 * Returns all users from the database.
	 */
	UserList getUserList() throws SQLException;
	/**
	 * Returns all users matching the search term.
	 */
	UserList getUserList(String term) throws SQLException;
	/**
	 * Returns a list of all users having this contact.
	 */
	UserList getUsersWithContact(IUser user) throws SQLException;
	/**
	 * Returns all contacts from the user object.
	 */
	ContactList getContactList(IUser user) throws SQLException;
	/**
	 * Checks to see if the password in the database
	 * matches the password given by the user.
	 */
	boolean checkPassword(IUser user, String password) throws SQLException;
	/**
	 * Returns all log messages from and to specified users.
	 * Also within specified time interval.
	 */
	MessageList getLogMessages(IUser from, IUser to, Date start, Date stop) throws SQLException;
	/**
	 * Returns all saved messages that has not been sent for the specified user
	 */
	MessageList getOfflineMessages(IUser user) throws SQLException;
	
	/***** Create methods ******/
	
	/**
	 * Adds a user to the database.
	 */
	void createUser(String firstName, String lastName, String email, String password) throws SQLException;
	/**
	 * Adds a contact to a user with a specific tag included in <code>Contact</code>.
	 * @see Contact
	 */
	void createContact(IUser user, Contact contact) throws SQLException;
	/**
	 * Adds a log message including information of the user
	 * who sends it and the user who receives it.
	 * Also includes the date when sent.
	 */
	void addLogMessage(Message message) throws SQLException;
	
	/* Update methods */
	
	/**
	 * Updates a user row with specified parameters.
	 * @param password 
	 * @param active 
	 */
	void updateUser(IUser user, String firstName, String lastName, String email, String password, boolean active) throws SQLException;
	/**
	 * Changes the tag of a contact by a user.
	 * @see Contact
	 */
	void updateTag(IUser user, Contact contact) throws SQLException;
	
	/***** Delete methods ******/
	
	/**
	 * Sets a user to inactive in database.
	 * If removed permanently, all dependencies will be ruined.
	 */
	void deleteUser(IUser user) throws SQLException;
	/**
	 * Removes a contact from a user's contactlist.
	 */
	void removeContact(IUser currentUser, IUser contact) throws SQLException;
	
}
